package com.ohuang.jiaxiaotong.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bunny.android.library.LoadDataLayout;
import com.example.myhttp.CallBackObjects;
import com.example.myhttp.CallBackString;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.Activity.LoginActivity;
import com.ohuang.jiaxiaotong.Activity.UploadNoticeActivity;
import com.ohuang.jiaxiaotong.MainActivity;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.EventMsgCode;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.Util.Sys;
import com.ohuang.jiaxiaotong.base.LazyFragment;
import com.ohuang.jiaxiaotong.entity.ClassNotice;
import com.ohuang.jiaxiaotong.entity.EventMsg;
import com.ohuang.jiaxiaotong.entity.User;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;


public class NoticeFragment extends LazyFragment {
    private String className;

    private ListView listView;
    private Button button_publish;
    private CommonAdapter commonAdapter;
    private List<ClassNotice> mData;
    private Dialog dialog;
    private LoadDataLayout loadDataLayout;


    public NoticeFragment() {
        // Required empty public constructor
    }

    public NoticeFragment(String className) {
        this.className = className;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }



    @Override
    protected void initView(View root) {
        initView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_notice;
    }

    @Override
    public void startLoad() {

        HttpGetClassNoticeList();
    }

    private void initView() {
        listView = view.findViewById(R.id.listView_notice);
        loadDataLayout = view.findViewById(R.id.loadData_notice);
        button_publish = view.findViewById(R.id.btn_uploadNotice_notice);
        button_publish.setOnClickListener(v -> {
            if (Sys.isClick()) {
                Intent intent = new Intent(getContext(), UploadNoticeActivity.class);
                intent.putExtra("className", className);
                startActivity(intent);
            }
        });

        loadDataLayout.showLoading("正在努力加载中", new LoadDataLayout.SetImgCallBack() {
            @Override
            public void setImg(ImageView img) {
                Glide.with(getActivity())
                        .load(R.drawable.gif_loading)
                        .into(img);
            }
        });//显示 load布局 状态正在加载
        listView.setAdapter(null);
    }

    private void HttpGetClassNoticeList() {

        HashMap<String, String> map = new HashMap<>();
        map.put("className", className);
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "user/findAllNotice", map, ClassNotice.class, new CallBackObjects<ClassNotice>() {
            @Override
            public void success(List<ClassNotice> ojb) {
                mData = ojb;
                if (mData != null) {
                    if (mData.size() == 0) {
                        loadDataLayout.showEmpty();
                    } else {
                        loadDataLayout.showSuccess();
                    }
                    commonAdapter = new CommonAdapter(getContext(), R.layout.list_notice_item, mData) {
                        @Override
                        protected void convert(ViewHolder viewHolder, Object item, int position) {
                            viewHolder.setText(R.id.txt_time_list_notice, ((ClassNotice) item).getNoticeTime());
                            viewHolder.setText(R.id.txt_content_list_notice, ((ClassNotice) item).getContent());
                            viewHolder.setOnClickListener(R.id.txt_delete_list_notice, v -> {
                                dialog = Ohuang.DeleteDialogShow(getContext(), "删除公告", "你确实要删除该公告吗？"
                                        , v1 -> {
                                            httpDeleteNotice(((ClassNotice) item).getId() + "");
                                            dialog.dismiss();
                                        });
                            });
                        }
                    };
                }
                listView.setAdapter(commonAdapter);
            }



            @Override
            public void fail(String s) {
                loadDataLayout.showError();
            }
        });

    }

    private void httpDeleteNotice(String id) {

        HashMap<String,String> map=new HashMap<>();
        map.put("id", id);
        map.put("className", className);
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "teacher/deleteNotice", map, new CallBackString() {
            @Override
            public void success(String ojb) {
                String code = ojb;
                if ("success".equals(code)) {
                    HttpGetClassNoticeList();
                } else {

                }
            }



            @Override
            public void fail(String s) {

            }
        });



    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void GetUserEventBus(EventMsg eventMsg) {
        if (eventMsg.getCode() == EventMsgCode.CODE_4) {
            HttpGetClassNoticeList();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }



}
